<?php

/**
 * 购物车控制器
 */
class CartController extends CommonController {

	/**
	 * 显示购物车首页
	 */
	public function index() {
		//			$carts =
		$carts = Cart::getAllData();
//		p($carts);
		session('iswrite',0);
		$this -> assign('carts', $carts);

		$this -> display();
	}

	/**
	 * 更新商品数量
	 */
	public function updateGoods() {
		//		 	var_dump(Q('post.'));
		//商品新数量
		$num = Q('post.num');
		//商品ID
		$cartId = Q('post.cartid');
		$data = array('sid' => $cartId, 'num' => $num);
		Cart::update($data);
		$allGoods = Cart::getAllData();
		//			var_dump($allGoods);
		$currentGoods = $allGoods['goods'][$cartId];
		//其他数据
		$newData = array('data' => $currentGoods,
		//商品总数量
		'count' => $allGoods['total_rows'],
		//新总价格
		'countMoney' => $allGoods['total']);

		$this -> ajax(array('status' => 1, 'data' => $newData));

	}

	/**
	 * AJAX获得所有的商品
	 */
	public function ajaxGoods() {
		if (!IS_AJAX)
			$this -> error('非法请求');
		$allGoods = Cart::getAllData();
		$this -> ajax($allGoods);
	}

	/**
	 * 确认定单
	 */
	public function shopConfirm() {
		if(!session('uid')){
			$this->error('请您先登陆',U('Login/index'));
		}
		if(!Cart::getGoods()){
			$this->error('亲您的购物车还是空的哦',U('Index/index'));
		}
	
		//当前选中的商品
		if(IS_POST){
				//获得当前用户的收货地址
			$uid = session('uid');
			//临时保存当前要结算的商品
			$tmpGoods = array();
			$takeAddr = K('ReceiveAddr') -> getUserRa($uid);
			$this -> assign('takeAddr', $takeAddr);
			$selectPro = Q('post.selectPro');
			$this -> assign('selectGoods', $selectPro);
	
			//购物车中所有的商品
			$cartGoods = Cart::getAllData();
			foreach ($selectPro as $k => $v) {
				foreach ($cartGoods['goods'] as $sid => $arr) {
					if ($v == $sid) {
						$tmpGoods['goods'][$sid] = $cartGoods['goods'][$sid];
					}
				}
			}
			//计算已经选中的商品的价格和数量
			$goodsCounts = array();
			foreach ($tmpGoods['goods'] as $sid => $arr) {
				$goodsCounts['countNum'][] = $arr['num'];
				$goodsCounts['countMoney'][] = $arr['total'];
			}
			$goodsCounts['countNum'] = array_sum($goodsCounts['countNum']);
			$goodsCounts['countMoney'] = array_sum($goodsCounts['countMoney']);
	
			$this -> assign('countGoods', $goodsCounts);
			//实例化城市模型
			$city = K('City');
			//查询到所有的省份
			$this -> assign('prov', $city -> getAllProvince());
			$this -> assign('cartGoods', $tmpGoods);
			$this -> display();
		}else{
			$this->error('非法请求','Index/index');
		}
		
	}

	/**
	 * 提交定单方法
	 */
	public function subOrder() {
		if (IS_POST) {
			if (!session('?uid')) {
				$this -> error('请您先登陆再提交订单');
			}

			//防止 重复提交
			if(!session('?iswrite') || session('iswrite')!=1){
				//实例化订单模型
				$model = K('GoodsOrder');
				
				$orderData = $model -> createOrder();
				if (!$orderData) {
					$this -> error($model -> error);
				} else {
					//将商品写入商品列表
					$orderlist = K('OrderList');
					if($orderlist->writeGoodsList($orderData)){
						//如果写入成功后将相应的购物车内的商品删除
						$orderList = Q('post.orderList'); //要结算的商品
						//获得当前购物车中所有的商品
						$cartData = Cart::getAllData();
						$allGoods = $cartData['goods'];
						//删除已经下单的购物车中的商品
						foreach($orderList as $sid){
							$tmp = array(
								'sid'=>$sid,
								'num'=>0
							);
							Cart::update($tmp);
						}
						//当前提交到订单的商品					
						$subGoods = Q('post.orderList');
						$this->assign('orderdata',$orderData);
						$this -> display();	
					}else{
						$this->error($orderlist->error,U('Cart/index'));
					}
				}
			}else{
				//写入重置
				session('iswrite',0);
				go('Cart/index');
			}
			
			
		}

	}

	/**
	 * 删除购物车中的内容
	 */
	public function delCart() {
		if (!IS_AJAX)
			$this -> error('非法请求');
		$cartId = Q('cartid', '', 'strval');
		//如果是一个数组那么表示要删除多个商品否则就删除单个商品
		$cartIds = json_decode($cartId);
		if (is_array($cartIds)) {
			foreach ($cartIds as $v) {
				if (!$v)
					continue;
				//删除单个商品
				$data = array('sid' => $v, 'num' => 0);
				Cart::update($data);
			}
			$this -> ajax(array('message' => '删除成功', 'status' => 1, 'data' => Cart::getAllData()));
		} else {
			//删除单个商品
			$data = array('sid' => $cartId, 'num' => 0);
			Cart::update($data);
			$this -> ajax(array('message' => '删除成功', 'status' => 1, 'data' => Cart::getAllData()));
		}

	}

	/**
	 * ajax添加购物车
	 */
	public function addCart() {
		if (!IS_AJAX)
			$this -> error('非法请求');
		//获得当前提交过来的商品内容
		$data = Q('post.', '', 'strval');
		$data['combinestr'] = explode(',', $data['combinestr']);
		$goods = array('id' => $data['gid'], 'name' => $data['goodsname'], 'num' => $data['goodscount'], 'img' => $data['goodsimg'], 'price' => $data['price'], 'options' => array('combine' => $data['combine'], 'combinestr' => $data['combinestr']), );

		$goods_number = K('GoodsList') -> where("combine='{$data['combine']}'") -> getField('goods_number');
		//查询当前组合是否有货
		if ($goods_number == 0) {
			$this -> ajax(array('message' => '对不起目前这个商品规格没有货请重新选择', 'status' => 0));
		} else {
			Cart::add($goods);
			//返回购物车商品总数
			$callData['total'] = $_SESSION['cart']['total_rows'];
			//返回购物车商品总价格
			$callData['totalmoney'] = $_SESSION['cart']['total'];
			$this -> ajax(array('message' => '添加购物车成功', 'data' => $callData, 'status' => 1));
		}
	}
	
	/**
	 * 支付成功页面
	 */
	public function pay(){

		$ordersn = $_REQUEST['sn'];
		if(K('GoodsOrder')->where("order_sn='{$ordersn}'")->save(array('order_status'=>'已付款'))){
			$this->success('付款成功',U('UserInfo/orderList'));
		}else{
			$this->error('付款失败');
		}
	}
	
	/**
	 * 确认收货
	 */
	 public function issure(){
	 	$oid = Q('get.oid',0,'intval');
		if(K('GoodsOrder')->where("oid=$oid")->save(array('order_status'=>'交易完成'))){
			$this->success('收货成功');
		}else{
			$this->error('收货失败');
		}
	 }

}
